extends Area2D

var parent_grab = false;
var collider_obj = false;

var catched_obj = false;
var do_grabing = false;

func _ready():
	if(get_parent()):
		print('ok');
	parent_grab = get_parent();
	collider_obj = get_node("claw_collision");

func _process(delta):
	if(catched_obj):
		var tar_vec = parent_grab.gear.position + position;
		var mid_vec = (catched_obj.position + tar_vec) / 2
		parent_grab.gear.position -= mid_vec - catched_obj.position
		catched_obj.position = mid_vec
	
	queue_redraw();
	if(do_grabing and not catched_obj):
		var objs = get_overlapping_areas();
		for obj in objs:
			if(obj.is_in_group('gear_group') and obj != parent_grab.gear):
				catched_obj = obj;
				queue_redraw();

func grab():
	if(not catched_obj):
		do_grabing = true;
		queue_redraw();

func drop():
	if(do_grabing or catched_obj):
		do_grabing = false;
		catched_obj = false;
		queue_redraw();

func _draw():
	
	draw_arc(Vector2.ZERO, 10, 0, PI*2, 36, Color.GRAY if(do_grabing) else Color.WHITE);
	if(catched_obj):
		var step = PI / 18;
		for i in range(0, PI*2, PI / 18):
			draw_arc(Vector2.ZERO, 20, i, PI / 18, 3, Color.AQUA);

func resize(x, isrelative = true):
	var _self = self;
	var grab = _self.parent_grab;
	if(isrelative): grab.claw_radius += x;
	else: grab.claw_radius = x;
	_self.collider_obj.shape.radius = grab.claw_radius
